//
//  MovieView.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-30.
//
//

#import "MovieView.h"
#import "DeviceUtil.h"
#import "NetworkAlertView.h"

@implementation MovieView
{
    NSMutableArray *_movieInfoArray;
    
    id<MovieListViewDelegate> _moviedelegate;
    id<MovieCoverViewDelegate> _covermoviedelegate;
    
    UISegmentedControl *_segment;
    MovieCoverView *_coverView;
    MovieListView *_listView;
    
    BOOL _segFlag;
}

- (void)dealloc
{
    [_movieInfoArray release];
    [_segment release];
    [_coverView release];
    [_listView release];
    
    _movieInfoArray = nil;
    _segment = nil;
    _coverView = nil;
    _listView = nil;
    [super dealloc];
    
}
- (id)initWithFrame:(CGRect)frame
        andDelegate:(id<MovieListViewDelegate>)moviedelegate
        coverDelegate:(id<MovieCoverViewDelegate>)covermoviedelegate
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        //等待网络
        NetworkAlertView *networking = [NetworkAlertView shareView];
        [networking showNetView];
        
        //请求当日上映电影
        _movieInfoArray = [[NSMutableArray alloc]init];
        MovieManager *moviemanager = [MovieManager shareManager];
        moviemanager.delegate = self;
        [moviemanager qryOnlineMovie];
        
        _moviedelegate = moviedelegate;
        _covermoviedelegate = covermoviedelegate;
        //初始化视图
        [self init_view];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

//初始化视图
- (void)init_view
{
    [self initSegmentCtrl];
    
    [self initCoverView];
    [self initListView];
    //[self addCoverView];
}

//初始化SegmentCtrl
- (void)initSegmentCtrl
{
    _segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], 30)];
    
    [_segment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [_segment setBackgroundColor:[UIColor lightGrayColor]];
    [_segment insertSegmentWithTitle:@"电影浏览" atIndex:0 animated:NO];
    [_segment insertSegmentWithTitle:@"电影展示" atIndex:1 animated:NO];
    
    [_segment addTarget:self action:@selector(onTabSelected:) forControlEvents:UIControlEventValueChanged];
    
    [self addSubview:_segment];
}

-(void)onTabSelected:(id)sender
{
    UISegmentedControl *control = (UISegmentedControl *)sender;
    
    switch (control.selectedSegmentIndex) {
        case 0:
            [self addCoverView];
            [self removeListView];
            break;
        case 1:
            [self removeCoverView];
            [self addListView];
            break;
        default:
            break;
    }
    
}

//封面展示
- (void)initCoverView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _coverView = [[MovieCoverView alloc] initWithFrame:CGRectMake(0, 30, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height - 110) andDelegate:_covermoviedelegate];
}
- (void)addCoverView
{
    if (_coverView)
    {
        _segFlag = 0;
        [self addSubview:_coverView];
    }
}
- (void)removeCoverView
{
    if (_coverView)
    {
        [_coverView removeFromSuperview];
    }
}

//列表展示
- (void)initListView
{
    float height = IOS7_OR_MORE?44.0:0.0f;
    _listView = [[MovieListView alloc] initWithFrame:CGRectMake(0, 30, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - height - 110) andDelegate:_moviedelegate];
}
- (void)addListView
{
    if (_listView)
    {
        _segFlag = 1;
        [self addSubview:_listView];
    }
}
- (void)removeListView
{
    if (_listView)
    {
        [_listView removeFromSuperview];
    }
}

#pragma network
- (void)qryOnlineMoviesFinish:(BOOL)isSuccess Movies:(NSMutableArray*)movieInfoArray;
{
    //取消等待
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking delView];
    
    _coverView.movieInfoArray = movieInfoArray;
    [_coverView.carousel reloadData];
    
    _listView.movieInfoArray = movieInfoArray;
    [self addListView];
}


@end




